package com.cty.Stack;
//用栈实现计算器，在ArrayStack上多添加一些功能
public class ArrayStack2 {
    private int maxSize;//最大长度
    private int Stack[];//数组
    private int top ;//栈顶指针

    //初始化数组
    public ArrayStack2(int maxSize) {
        this.maxSize = maxSize;
        Stack = new int[maxSize];
        top = -1;
    }

    //判空
    public boolean isEmpty() {
        return top == -1;
    }

    //判满方法
    public boolean isFull() {
        return top == maxSize - 1;
    }

    //添加方法
    public void push(int num) {
        if (isFull()) {
            System.out.println("栈满~~");
            return;
        }
        top++;
        Stack[top] = num;
    }

    //出栈方法
    public int pop() {
        if (isEmpty()) {
            throw new RuntimeException("栈空~~");
        }
        int value = Stack[top];
        top--;
        return value;
    }

    //遍历方法
    public void list() {
        if (isEmpty()) {
            System.out.println("栈空~~");
            return;
        }
        for (int i = top; i >= 0; i--) {
            System.out.println(Stack[i]);
        }
    }

    //返回栈顶元素
    public int peek() {
        if (isEmpty()) {
            throw new RuntimeException("栈空~~");
        }
        return Stack[top];
    }

    //判断是否为运算符
    public boolean isOper(char ch) {
        return ch == '+' || ch == '-' || ch == '*' || ch=='/';
    }

    //返回运算符的优先级
    public int oper(int ch) {
        if (ch == '*' || ch == '/') {
            return 1;
        }else if (ch == '-' || ch == '+') {
            return 0;
        }else {
            return -1;//不规则运算符
        }
    }

    //计算方法
    public int cal(int num1,int num2,int ch) {
        //接受结果
        int res = 0;
        //开始计算
        switch (ch) {
            case '+' :
                res = num1 + num2;
                break;
            case '-':
                res = num2 - num1;
                break;
            case '*':
                res = num1 * num2;
                break;
            case '/':
                res = num2 / num1;
                break;
            default:
                break;
        }
        return res;
    }
}